﻿/*
 * Copyright (C) 2009-2010 Valerio Fuoglio <valerio.fuoglio@gmail.com>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using Lastfm.Scrobbling;
using LightScrobbler.Properties;

namespace LightScrobbler
{
    internal partial class FileSummary : Form
    {
        private readonly string _logPath;
        private readonly List<Entry> _playedTracks;
        private readonly List<Entry> _skippedTracks;
        private readonly IViewer _viewer;
        private SendForm _sendForm;
        private ToolTip _sendToolTip;

        public FileSummary()
        {
            InitializeComponent();
            MinimumSize = Size;

            _playedTracks = ScrobbleSessionData.GetInstance().PlayedTracks;
            _skippedTracks = ScrobbleSessionData.GetInstance().SkippedTracks;
            _logPath = ScrobbleSessionData.GetInstance().FilePath;

            if (_playedTracks.Count < Settings.Default.DisplayThreshold)
            {
                _viewer = new Viewer(_playedTracks, true);
                tableLayoutPanel1.Controls.Add((Viewer) _viewer, 0, 0);
            }
            else
            {
                _viewer = new GridViewer(_playedTracks, true);
                tableLayoutPanel1.Controls.Add((GridViewer) _viewer, 0, 0);

                Resize +=
                    delegate(object sender, EventArgs e) { ((GridViewer) _viewer).Event_Resize(sender, e, WindowState); };
                ResizeEnd += ((GridViewer) _viewer).Event_ResizeEnd;
            }


            _sourceNameLabel.Text = _logPath;

            // ==== SKIPPED TRACKS
            _skippedButton.Enabled = (_skippedTracks.Count == 0) ? false : true;

            // ==== SEND BUTTON
            _sendButton.Enabled = (_playedTracks.Count == 0) ? false : true;
            ActiveControl = (_playedTracks.Count == 0) ? _cancelButton : _sendButton;


            // ==== STATUS BAR
            if (_playedTracks.Count == 0)
            {
                _summaryLabel.Text = "NO TRACK to scrobble... try to check Skipped tracks";
                ActiveControl = _skippedButton;
            }
            else
            {
                string thereIsAre = (_playedTracks.Count == 1) ? "is" : "are";
                string trackToken = (_playedTracks.Count == 1) ? "track" : "tracks";

                _summaryLabel.Text = "There " + thereIsAre + " " + _playedTracks.Count + " " + trackToken + " " +
                                     "to scrobble ... Let's get to work :P";

                ActiveControl = _sendButton;
            }

            ScrobbleSessionData ssd = ScrobbleSessionData.GetInstance();

            if (ssd.SessionState != SessionState.Authendicated)
            {
                /***** ToolTip doesn't display on a disabled button *****
                _sendToolTip = new ToolTip();
                _sendToolTip.AutoPopDelay = 3000;
                _sendToolTip.InitialDelay = 100;
                _sendToolTip.ReshowDelay = 500;
                _sendToolTip.ShowAlways = true;

                string tooltipString;
                switch (ssd.SessionState)
                {
                    case SessionState.AuthenticationFailed:
                        tooltipString = "Wrong username or password, cannot login to LastFm";
                        break;
                    case SessionState.NoNetwork:
                        tooltipString = "Network error detected";
                        break;
                    default:
                        tooltipString = "";
                        break;
                }
                _sendToolTip.SetToolTip(_sendButton, tooltipString);

                */

                _sendButton.Enabled = false;
            }

            Shown += AfterShown;
        }

        private void AfterShown(object sender, EventArgs e)
        {
            new Thread(_viewer.Populate).Start();
        }

        private void _skippedButton_Click(object sender, EventArgs e)
        {
            ScrobbleSessionData.GetInstance().DriveLocked = true;
            var st = new SkippedTracks(_skippedTracks);
            st.ShowDialog(this);
            ScrobbleSessionData.GetInstance().DriveLocked = false;
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            ClosingProcedure();
            Close();
        }

        private void ClosingProcedure()
        {
            _summaryLabel.Text = "Closing...";
            _viewer.Close();
        }

        private void _sendButton_Click(object sender, EventArgs e)
        {
            ScrobbleSessionData.GetInstance().SelectedTracks = _viewer.SelectedEntries;
            _sendForm = new SendForm();
            _sendForm.ShowDialog(this);

            if (ScrobbleSessionData.GetInstance().SuccessifullySent)
            {
                ClosingProcedure();
                Close();
            }
        }
    }
}